﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Wicresoft.Shell.Core;

namespace Wicresoft.Shell.Package.AppExp
{
    public partial class ExistConnectForm : Form
    {
        #region Fields

        private ConnectInfos m_CnnInfos;

        #endregion

        #region Constructor

        public ExistConnectForm()
        {
            InitializeComponent();
        }

        #endregion

        #region Methods

        private void InitialControlsState()
        {
            m_CnnInfos = ConnectInfos.LoadConnectInfos();

            ResetControlsState();

            if (lbDBList.Items.Count > 0)
                lbDBList.SelectedIndex = 0;
        }

        private void ResetControlsState()
        {
            btnOk.Enabled = false;
            btnRemoveConnect.Enabled = false;
            lbDBList.Items.Clear();
            foreach (ConnectInfoType eachCnnInfo in m_CnnInfos.ConnectInfo)
                lbDBList.Items.Add(eachCnnInfo);
        }

        private void lbDBList_SelectedIndexChanged(Object sender, EventArgs e)
        {
            if (lbDBList.SelectedIndex >= 0)
            {
                btnOk.Enabled = true;
                btnRemoveConnect.Enabled = true;
            }
            else
            {
                btnOk.Enabled = false;
                btnRemoveConnect.Enabled = false;
            }
        }

        private void btnOk_Click(Object sender, EventArgs e)
        {
            if (lbDBList.SelectedIndex < 0)
                return;

            DialogResult addConnectDlgResult = DialogResult.None; // get the DialogResult if password is not set in the ConnectInfo.
            ConnectInfoType selectedCnnInfo = (ConnectInfoType)lbDBList.SelectedItem;

            if (Workspace.Connect(selectedCnnInfo, out addConnectDlgResult))
            {
                m_CnnInfos.AddConnectInfo(selectedCnnInfo);

                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            else
            {
                if (DialogResult.Cancel != addConnectDlgResult)
                {
                    String errInfo;
                    if (!MySqlHelper.TestDBConnection(selectedCnnInfo.ServerName, selectedCnnInfo.DBName,
                                                     selectedCnnInfo.UserName, selectedCnnInfo.GetPasswordStr(),
                                                     out errInfo))
                    {
                        DialogResult dlgResult = MessageBox.Show(String.Format(Resources.ExistConnectForm_RemoveInvalidConnect, errInfo),
                                                                Resources.MsgBox_Failed, MessageBoxButtons.YesNo, MessageBoxIcon.Error);
                        if (DialogResult.Yes == dlgResult)
                        {
                            m_CnnInfos.RemoveConnectInfoByRef(selectedCnnInfo);
                            ResetControlsState();
                        }
                    }
                }
            }
        }

        private void btnCancel_Click(Object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void btnAddConnect_Click(Object sender, EventArgs e)
        {
            ConnectInfoType newCnnInfo = null;

            if (Workspace.NewConnect(out newCnnInfo))
            {
                m_CnnInfos.AddConnectInfo(newCnnInfo);
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }

        private void btnRemoveConnect_Click(object sender, EventArgs e)
        {
            DialogResult dlgResult = DialogResult.None;

            dlgResult = MessageBox.Show(Resources.ExistConnectForm_RemoveConnect, Resources.MsgBox_Info,
                                    MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (DialogResult.Yes == dlgResult)
            {
                m_CnnInfos.RemoveConnectInfoByRef((ConnectInfoType)lbDBList.SelectedItem);
                ResetControlsState();
            }
        }

        private void ExistConnectForm_Shown(object sender, EventArgs e)
        {
            InitialControlsState();
        }

        private void ExistConnectForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            m_CnnInfos.SaveConnectInfos();
        }

        #endregion

        #region Properties

        public ConnectInfoType SelectedConnectionInfo
        {
            get { return (ConnectInfoType)lbDBList.SelectedItem; }
        }

        #endregion
    }
}
